package com.example.sharesinfo.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sharesinfo.R;
import com.example.sharesinfo.base.BaseUIActivity;
import com.example.sharesinfo.entity.Constants;
import com.example.sharesinfo.helper.ActivityHelper;
import com.example.sharesinfo.manager.CloudManager;
import com.example.sharesinfo.manager.DialogManager;
import com.example.sharesinfo.service.CloudService;
import com.example.sharesinfo.utils.CleanMessageUtil;
import com.example.sharesinfo.utils.SpUtils;
import com.example.sharesinfo.view.DialogView;

import cn.bmob.v3.BmobUser;

public class SettingActivity extends BaseUIActivity implements View.OnClickListener {

    private Switch sw_app_tips;
    private RelativeLayout rl_app_tips;
    private RelativeLayout rl_change_password;
    private TextView tv_cache_size;
    private RelativeLayout rl_clear_cache;
    private DialogView mDaView;
    private DialogView mExitView;
    private TextView tv_new_version;
    private TextView tv_app_version;
    private RelativeLayout rl_check_version;
    private Button btn_logout;
    private ImageButton ib_return;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ActivityHelper.getInstance().addActivity(SettingActivity.this);
        initView();
        createDaDialog();
        createExitDialog();
    }

    private void initView() {
        ib_return = findViewById(R.id.ib_return);
        ib_return.setOnClickListener(this);
        sw_app_tips = findViewById(R.id.sw_app_tips);
        sw_app_tips.setOnClickListener(this);
        rl_app_tips = findViewById(R.id.rl_app_tips);
        rl_change_password = findViewById(R.id.rl_change_password);
        rl_change_password.setOnClickListener(this);
        tv_cache_size = findViewById(R.id.tv_cache_size);
        try {
            if (CleanMessageUtil.getTotalCacheSize(getApplication()).equals("ok")) {
                tv_cache_size.setText("0kb");
            } else {
                tv_cache_size.setText(CleanMessageUtil.getTotalCacheSize(getApplication()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        rl_clear_cache = findViewById(R.id.rl_clear_cache);
        rl_clear_cache.setOnClickListener(this);
        tv_new_version = findViewById(R.id.tv_new_version);
        tv_app_version = findViewById(R.id.tv_app_version);
        rl_check_version = findViewById(R.id.rl_check_version);
        btn_logout = findViewById(R.id.btn_logout);
        btn_logout.setOnClickListener(this);
    }

    /**
     * @type
     * @explain 创建清除内存提示框
     * @author
     * @creat time
     */

    private void createDaDialog() {
        mDaView = DialogManager.getInstance().initView(this,
                R.layout.dialog_clear_cache);
        //外部点击无效
        mDaView.setCancelable(false);
        TextView tv_activate = mDaView.findViewById(R.id.tv_activate);//取消
        TextView tv_sure = mDaView.findViewById(R.id.tv_sure);//确认
        tv_activate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogManager.getInstance().hide(mDaView);
            }
        });
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CleanMessageUtil.clearAllCache(getApplication());
                Toast.makeText(SettingActivity.this, "缓存清理完毕", Toast.LENGTH_SHORT).show();
                DialogManager.getInstance().hide(mDaView);
                tv_cache_size.setText("0kb");
            }
        });
    }
    /**
     * @type
     * @explain 创建退出登陆提示框
     * @author
     * @creat time
     */

    private void createExitDialog() {
        mExitView = DialogManager.getInstance().initView(this,
                R.layout.dialog_exit);
        //外部点击无效
        mExitView.setCancelable(false);
        TextView tv_activate = mExitView.findViewById(R.id.tv_activate);//取消
        TextView tv_sure = mExitView.findViewById(R.id.tv_sure);//确认
        tv_activate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogManager.getInstance().hide(mExitView);
            }
        });
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //处理退出逻辑
                logout();
            }
        });
    }
    /**
     * 退出登录
     */
    private void logout() {
        //删除Token
        SpUtils.getInstance().deleteKey(Constants.SP_TOKEN);
        SpUtils.getInstance().putBoolean(Constants.SP_IS_LOGIN, false);
        //Bmob退出登录
        BmobUser.logOut();
        //融云
        CloudManager.getInstance().disconnect();
        CloudManager.getInstance().logout();
        //退出所有的activity栈
        ActivityHelper.getInstance().exit();
        stopService(new Intent(this, CloudService.class));
        //跳转到登录页
        Intent intent_login = new Intent();
        intent_login.setClass(SettingActivity.this, LoginActivity.class);
        intent_login.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent_login);
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.ib_return:
                finish();
                break;
            case R.id.sw_app_tips:
                break;
            case R.id.rl_change_password:
                break;
            case R.id.rl_clear_cache:
                DialogManager.getInstance().show(mDaView);
                break;
            case R.id.btn_logout:
                DialogManager.getInstance().show(mExitView);
                break;
        }
    }
}
